package com.wxdc.nmgyt.nmgytwydc.presenter;

import com.wxdc.nmgyt.nmgytwydc.base.RxPersenter;
import com.wxdc.nmgyt.nmgytwydc.http.RetrofitHelper;
import com.wxdc.nmgyt.nmgytwydc.model.BASE_WORKFLOWCURRENT;
import com.wxdc.nmgyt.nmgytwydc.model.COMMIT_AUDIT;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.AuditResultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.AuditRolResultBean;
import com.wxdc.nmgyt.nmgytwydc.model.backresult.ValidateJcdInfoResultBean;
import com.wxdc.nmgyt.nmgytwydc.presenter.contract.AuditContract;

import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observer;
import io.reactivex.Scheduler;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/11/13.
 * 审核功能
 */

public class AuditPersenter extends RxPersenter<AuditContract.View> implements AuditContract.Persenter {
    private RetrofitHelper mRetrofitHelper;
    @Inject
    public AuditPersenter(RetrofitHelper retrofitHelper) {
        mRetrofitHelper = retrofitHelper;
    }

    /**
     * 获取当前用户的审核权限
     */
    @Override
    public void getUserAuditRol() {
        mRetrofitHelper.getUserAuditRol()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<AuditRolResultBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<AuditRolResultBean> auditRolResultBeans) {
                        mView.userAuditRolResult(auditRolResultBeans);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void findAuditData(int id) {
        mRetrofitHelper.getAuditData(id)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<BASE_WORKFLOWCURRENT>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(List<BASE_WORKFLOWCURRENT> base_workflowcurrents) {
                        if (id == 0) {
                            mView.getBackOrWaitAuditDataResult(base_workflowcurrents);
                        } else if (id == 2) {
                            mView.getAuditDataResult(base_workflowcurrents);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void commitAudit(COMMIT_AUDIT commit_audit) {
        mRetrofitHelper.commitAudit(commit_audit)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<AuditResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(AuditResultBean resultBean) {
                        mView.getCommitAuditResult(resultBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    @Override
    public void validateIsYdInfo(String gempointID) {
        mRetrofitHelper.validateYdInfo(gempointID)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ValidateJcdInfoResultBean>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                    }

                    @Override
                    public void onNext(ValidateJcdInfoResultBean validateJcdInfoResultBean) {
                        mView.getYdInfoValidateResult(validateJcdInfoResultBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("error");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
